package util;

import java.util.Random;
import org.testng.annotations.Test;

/**
 * 操作系统的彩票调度算法
 *
 * @author
 */
public class Schedule {

    private int[] w;

    /**
     * 彩票总数量
     */
    private int size = 0;
    private static final Random RND = new Random();

    public Schedule(int[] w) {
        this.w = w;
        for (int n : w) {
            size += n;
        }
    }

    /**
     *
     *
     * @return
     */
    public int pickIndex() {
        int r = RND.nextInt(size);
        int sum = 0;
        for (int i = 0; i < size; ++i) {
            if ((sum += w[i]) > r) {
                return i;
            }
        }
        return 0;
    }


}
